//
//  PublishView.swift
//  个人微博
//
//  Created by guo3920591 on 16/4/25.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

import UIKit
import pop

class PublishView: UIView {
    
  weak  var tagret: UIViewController?
    
 

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置界面
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 移动到父控件
    override func didMoveToSuperview() {
        super.didMoveToSuperview()
        
       print(subviews.count)
        
    for i in  2..<subviews.count{
        
        let subView = subviews[i]
        
        let anmi = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        
            anmi.toValue = NSValue(CGPoint: CGPoint(x: subView.center.x, y: subView.center.y - 400))
            
            anmi.beginTime = CACurrentMediaTime() + Double(i)*0.1
        
         anmi.springBounciness=7;
        
          anmi.springSpeed=7;
            
            subView.pop_addAnimation(anmi, forKey: "anmi")
        

    
    }
    }
    //私有函数
    
    
    @objc private func btnClickBig(btn:UIButton){
        
  UIView.animateWithDuration(0.25, animations: { () -> Void in
    
    for i in 2..<self.subviews.count{
        
        self.subviews[i].alpha = 0.01
        
        if self.subviews[i] == btn {
            
            btn.transform = CGAffineTransformMakeScale(1.5, 1.5)
            
        }else{
            
            self.subviews[i].transform = CGAffineTransformMakeScale(0.01, 0.01)
        }
        
    }
    
    
    }) { (_) -> Void in
        
        let vc = PublishViewController()
        
        self.tagret?.presentViewController(UINavigationController(rootViewController: vc), animated: true, completion: { () -> Void in
            
            self.removeFromSuperview()
            
        })
        
        
        
        }
        
        
    }
    
    //创建按钮
    private func createButtonMore(){
        
        
        let iconArr = ["tabbar_compose_idea","tabbar_compose_photo","tabbar_compose_camera","tabbar_compose_lbs","tabbar_compose_review","tabbar_compose_more"]
        
        let lbTitle = ["文字","相册","拍摄","签到","点评","更多"]
        
        let btnW: CGFloat = 71
        
        let btnH: CGFloat = 100
        
        let marginBtn  = (UIScreen.mainScreen().bounds.width-3*btnW)/4
        
        for i in 0..<iconArr.count{
            
            let btn = XLUpDownButton()
            
            btn.addTarget(self, action: "btnClickBig:", forControlEvents: .TouchUpInside)
            
             btn.setTitle(lbTitle[i], forState: .Normal)
            
             btn.setImage(UIImage(named: iconArr[i]), forState: .Normal)
            
             btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
            
             let col = CGFloat(i%3)
            
             let row = CGFloat(i/3)
            
             let btnX = marginBtn + col * (marginBtn + btnW)
            
             let btnY = row * (marginBtn + btnH) + UIScreen.mainScreen().bounds.height
            
            
             btn.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH)
            
            addSubview(btn)
            
            
        }
        
        
        
        
        
    
    
    }
    
    
    // MARK: - 私有控件
    
    private lazy var titlelbImgView: UIImageView = UIImageView(imageName: "compose_slogan")
    
    /// 公共函数
    
    func showPublish(tagert: UIViewController?){
        
        tagert?.view.addSubview(self)
        
        self.tagret = tagert
        
    }
    /// 点击屏幕让其退出
   
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        var index = 6
        
        for i in  2..<subviews.count{
            
            let subView = subviews[i]
            
            let anmi = POPSpringAnimation(propertyNamed: kPOPViewCenter)
            
            anmi.toValue = NSValue(CGPoint: CGPoint(x: subView.center.x, y: subView.center.y + 400))
            
            anmi.beginTime = CACurrentMediaTime() + Double(index--)*0.1
            
            anmi.springBounciness=7;
            
            anmi.springSpeed=7;
            
            subView.pop_addAnimation(anmi, forKey: "anmi")
            
            
            
        }
        
        
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.4 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) { () -> Void in
           
                self.removeFromSuperview()
      
            
        }
        
    }
    
    
    deinit{
        
        print("我销毁了")
    }
    
    
}
// MARK: - 设置界面
extension PublishView{
    
    private func setupUI(){
        //设置位置
        
       // backgroundColor = UIColor.yellowColor()
        frame = UIScreen.mainScreen().bounds
        
        let img = UIImage.xl_screenshots().applyLightEffect()
        
        let imgVc = UIImageView(image: img)
        
//        let data = UIImagePNGRepresentation(img)
//        data?.writeToFile("/Users/guo3920591/Desktop/汉化.png", atomically: true)
        
        addSubview(imgVc)
        
        //添加标题栏
        
        addSubview(titlelbImgView)
        
        //添加按钮
        
        createButtonMore()
        
        //设置约束
        
        titlelbImgView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-150)
            
        }
        
        
        
        
    }
    
    

    
    

}
